#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
调整图标尺寸
"""

import os

def resize_icon():
    """调整图标尺寸为64x64"""
    try:
        from PIL import Image
        
        # 打开JPEG文件
        with Image.open('keyboard_icon.jpeg') as img:
            print(f"原始尺寸: {img.size}")
            
            # 调整尺寸为64x64，保持宽高比
            img_resized = img.resize((64, 64), Image.Resampling.LANCZOS)
            
            # 保存为PNG格式（桌面图标推荐使用PNG）
            img_resized.save('keyboard_icon_64.png')
            print("✅ 图标已调整为64x64并保存为PNG格式")
            
            # 同时保存为JPEG格式（如果用户坚持要JPEG）
            img_resized.save('keyboard_icon_64.jpeg', 'JPEG', quality=95)
            print("✅ 图标已调整为64x64并保存为JPEG格式")
            
            return True
            
    except ImportError:
        print("❌ 缺少PIL库，无法调整图片尺寸")
        print("请安装: pip install Pillow")
        return False
    except Exception as e:
        print(f"❌ 调整图片尺寸失败: {e}")
        return False

def create_simple_icon():
    """创建一个简单的64x64图标"""
    try:
        from PIL import Image, ImageDraw, ImageFont
        
        # 创建64x64的图标
        img = Image.new('RGBA', (64, 64), (0, 0, 0, 0))
        draw = ImageDraw.Draw(img)
        
        # 背景圆形
        draw.ellipse([2, 2, 62, 62], fill=(52, 152, 219), outline=(41, 128, 185), width=2)
        
        # 绘制键盘按键
        # 第一行
        draw.rectangle([8, 12, 18, 22], fill=(255, 255, 255), outline=(0, 0, 0))
        draw.rectangle([20, 12, 30, 22], fill=(255, 255, 255), outline=(0, 0, 0))
        draw.rectangle([32, 12, 42, 22], fill=(255, 255, 255), outline=(0, 0, 0))
        draw.rectangle([44, 12, 54, 22], fill=(255, 255, 255), outline=(0, 0, 0))
        
        # 第二行
        draw.rectangle([8, 24, 18, 34], fill=(255, 255, 255), outline=(0, 0, 0))
        draw.rectangle([20, 24, 30, 34], fill=(255, 255, 255), outline=(0, 0, 0))
        draw.rectangle([32, 24, 42, 34], fill=(255, 255, 255), outline=(0, 0, 0))
        draw.rectangle([44, 24, 54, 34], fill=(255, 255, 255), outline=(0, 0, 0))
        
        # 第三行
        draw.rectangle([8, 36, 18, 46], fill=(255, 255, 255), outline=(0, 0, 0))
        draw.rectangle([20, 36, 30, 46], fill=(255, 255, 255), outline=(0, 0, 0))
        draw.rectangle([32, 36, 42, 46], fill=(255, 255, 255), outline=(0, 0, 0))
        draw.rectangle([44, 36, 54, 46], fill=(255, 255, 255), outline=(0, 0, 0))
        
        # 添加音符符号
        draw.ellipse([48, 8, 52, 12], fill=(255, 255, 0))
        draw.ellipse([50, 10, 54, 14], fill=(255, 255, 0))
        
        # 保存为PNG
        img.save('keyboard_icon_64.png')
        
        # 转换为RGB模式后保存为JPEG
        img_rgb = img.convert('RGB')
        img_rgb.save('keyboard_icon_64.jpeg', 'JPEG', quality=95)
        print("✅ 创建了新的64x64图标")
        return True
        
    except ImportError:
        print("❌ 缺少PIL库")
        return False
    except Exception as e:
        print(f"❌ 创建图标失败: {e}")
        return False

if __name__ == "__main__":
    print("🎨 调整图标尺寸...")
    
    # 首先尝试调整现有JPEG文件
    if os.path.exists('keyboard_icon.jpeg'):
        if resize_icon():
            print("✅ 图标调整完成")
        else:
            print("⚠️ 调整失败，创建新图标...")
            create_simple_icon()
    else:
        print("⚠️ 未找到keyboard_icon.jpeg，创建新图标...")
        create_simple_icon()
    
    print("🎉 图标处理完成！")
